let params = this.getVariables().find(v => v.name === "screen_params")?.value;
const prefix = this.getVariables().find(v => v.name === "environmentType")?.value;
const search = this.getApiWidgetByAliasName('search')?.widgetObj;
const eventTitle = this.getApiWidgetByAliasName('eventTitle')?.widgetObj;
const list = this.getApiWidgetByAliasName('list')?.widgetObj;

try {
  params = JSON.parse(params);
} catch (error) {
  console.error(error)
}

function baseFetch(url, params) {
  return fetch(url, {
    method: 'POST',
    headers: { "Content-type": "application/json" },
    body: JSON.stringify(params),
  }).then(res => res.json())
}

if(params) {
  eventTitle._option.links.text = params.eventTitle;
  eventTitle.render();
  list.setEvent(params);
  params.eventType = params.eventTypeCode;
  params.eventLevel = params.eventGradeCode;
  baseFetch(`/${prefix}SummaryAssessmentSV/emergencyHandle/queryEmergencyPlan`, params).then(({ success, result }) => {
    if(success && Array.isArray(result)) {
      const planArr = result?.map((v, i) => ({
        ...v,
        name: v.planName + (i === 0 ? '(推荐)' : ''),
        value: v.id
      }));

      if (planArr.length) {
        list.setParams(arr[0].id, params.eventGradeCode);
        // list.setParams(arr[0].id, '1692442354007216130');
        // console.log('name+++++', arr)
        search.asyncSelectOptions('name', arr);
        search.setFormData('form_1', {
          name: arr[0].id,
          level: params.eventGradeCode
        });

        list.initQuery()
      } else {
        layer?.msg('未获取到适合的预案！')
      }
    }
  })
}
